package controller

import (
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"mynetdisk/model"
	"mynetdisk/utils"
	"net/http"
)

/**
 *@Author tudou
 *@Date 2019/8/24
 **/



//用户将文件设置为可公开文件
func SetFilePublic(c *gin.Context){
	id:=utils.ParseStringToUint(c.PostForm("id"))
	session:=sessions.Default(c)
	username:=session.Get("username").(string)
	var file model.File
	if !file.GetFile(id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"there is an unknown error"})
		return
	}
	if !file.UpdateFile(username,0,id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"there is an unknown error"})
		return
	}
	c.JSON(http.StatusOK,gin.H{"message":"update successfully"})
}

func SetFilePersonal(c *gin.Context){
	id:=utils.ParseStringToUint(c.PostForm("id"))
	session:=sessions.Default(c)
	username:=session.Get("username").(string)
	userId:=session.Get("user").(uint)
	var file model.File
	if !file.GetFile(id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"there is an unknown error"})
		return
	}
	if !file.UpdateFile(username,userId,id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"there is an unknown error"})
		return
	}
	c.JSON(http.StatusOK,gin.H{"message":"update successfully"})
}